using System;
using System.Collections.Generic;
using System.Text;

namespace ControlPoint.ControlAPI {
    public class SettingsList {
        SerializableDictionary<string, Setting> _settings;

        public event EventHandler<SettingChangedEventArgs> SettingChanged;

        public SettingsList() {
            this._settings = new SerializableDictionary<string, Setting>();
        }

        public SettingsList(SerializableDictionary<string, Setting> settings) {
            this._settings = new SerializableDictionary<string, Setting>(settings);
        }

        public SettingsList(List<Setting> ls) {
            this._settings = new SerializableDictionary<string, Setting>();
            foreach (Setting s in ls) {
                this._settings.Add(s.Name, s);
            }
        }

        public SerializableDictionary<string, Setting> Settings {
            get {
                return this._settings;
            }
            set {
                this._settings = new SerializableDictionary<string, Setting>(value);
            }
        }

        public bool ContainsSetting(string name) {
            return this._settings.ContainsKey(name);
        }

        public Setting GetSetting(string name) {
            return this._settings[name];
        }

        public void SetSetting(Setting setting) {
            this._settings.Add(setting.Name, setting);
            if (this.SettingChanged != null) {
                this.SettingChanged(this, new SettingChangedEventArgs(setting));
            }
        }

        public void SetSetting(string name, object value) {
            Setting s;
            if (this._settings.ContainsKey(name)) {
                s = this._settings[name];
                s.Value = value;
                this._settings[name] = s;
            } else {
                s = new Setting(name, value);
                this._settings.Add(name,s );
            }
            if (this.SettingChanged != null) {
                this.SettingChanged(this, new SettingChangedEventArgs(s));
            }
        }

        public override string ToString() {
            string r = "";
            foreach (Setting s in this._settings.Values) {
                r = r + s.Name + ": " + s.Value.ToString() + Environment.NewLine;
            }
            return r;
        }
    }
}
